package com.lepu.app.fun.index.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.utils.Setting;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.knowledge.bean.Article;

public class IndexHotArticleAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	@SuppressWarnings("unused")
	private Context mContext = null;
	private List<Article> mArrayList = null;

	public IndexHotArticleAdapter(Context context, List<Article> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.index_article_list_item, null);

			holder = new ViewHolder();

			TextView tagTextView = (TextView) convertView
					.findViewById(R.id.tagTextView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			TextView infoTextView = (TextView) convertView
					.findViewById(R.id.infoTextView);
			ImageView imageView = (ImageView) convertView
					.findViewById(R.id.imageView);
			ImageView playImageView = (ImageView) convertView
					.findViewById(R.id.playImageView);
			TextView timeTextView = (TextView) convertView
					.findViewById(R.id.timeTextView);

			holder.tagTextView = tagTextView;
			holder.titleTextView = titleTextView;
			holder.infoTextView = infoTextView;
			holder.imageView = imageView;
			holder.playImageView = playImageView;
			holder.timeTextView = timeTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Article item = mArrayList.get(position);

		holder.tagTextView.setText(item.ArticleTag);
		holder.titleTextView.setText(item.ArticleTitle);
		holder.infoTextView.setText(item.ArticleDescription);
		
		UIHelper.setTextViewLines(holder.infoTextView, 2);

		String imageUrl = Setting.getImageUrl(item.Cover);
		UniversalImageLoadTool.disPlay(imageUrl, holder.imageView,
				R.drawable.app_icon_defaut_1_1);

		int ContentType = UtilityBase.parseInt(item.ContentType);
		if (ContentType == 2) {
			// video
			holder.playImageView.setVisibility(View.VISIBLE);
		} else {
			// article
			holder.playImageView.setVisibility(View.GONE);
		}
		
		String time = DateUtilBase.stringFromDate(DateUtilBase.dateFromString(
				item.PublishDate, "yyyy-MM-dd HH:mm:ss"),
				"yyyy-MM-dd HH:mm");
		holder.timeTextView.setText(time);

		return convertView;
	}

	private static class ViewHolder {
		TextView tagTextView;
		TextView titleTextView;
		TextView infoTextView;
		ImageView imageView;
		ImageView playImageView;
		TextView timeTextView;
	}
}
